package br.edu.unifas.es5.bd2.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/*
 * Comentario para testar o commit 5
 */
public class Main {

	public static void main(String[] args) {
		List<Aluno> alunos = new ArrayList<Aluno>();
		try {
			Class.forName("org.postgresql.Driver");
			
			Calendar cal = Calendar.getInstance();
			cal.set(Calendar.YEAR, 1993);
			cal.set(Calendar.MONTH, Calendar.JANUARY);
			cal.set(Calendar.DAY_OF_MONTH, 1);
			
			Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/aluno", "aluno", "aluno");
			PreparedStatement ps = conn.prepareStatement("select nome, matricula, nascimento from aluno where nascimento >= ?");
			ps.setTimestamp(1, new Timestamp(cal.getTimeInMillis()));
			ResultSet rs = ps.executeQuery();
			while(rs.next()) {
				Aluno aluno = new Aluno();
				aluno.setMatricula(rs.getInt(2));
				aluno.setNome(rs.getString(1));
				aluno.setNascimento(new Date(rs.getTimestamp(3).getTime()));
				alunos.add(aluno);
			}
			ps.close();
			rs.close();
			conn.close();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		for (Aluno aluno : alunos) {
			System.out.println(aluno);
		}
	}
}
